import { jsapi, layerCreator, viewUtils, layerUtils } from '@/utils/arcgis';

const [hotSym, yellowSym, greenSym, otherSym] = [
  [255, 0, 0, 1], // 红
  [255, 215, 0, 1], // 黄
  [78, 242, 183, 0.9], // 绿
  [255, 255, 255, 0.2],
].map(color => {
  return {
    type: 'mesh-3d', // autocasts as new MeshSymbol3D()
    symbolLayers: [
      {
        type: 'fill', // autocasts as new FillSymbol3DLayer()
        material: { color },
      },
    ],
  };
});

// 添加 building scenlayer
const add = async (view, opt) => {
  const currentLayer = await layerCreator.create(opt);
  // console.log(currentLayer)
  view.map.add(currentLayer);
  // setFullModel(currentLayer);
  // layerUtils.zoomTolayer(view, currentLayer);
};

// 将building所有构件显示
const setFullModel = layer => {
  if (layer) {
    layer.when(() => {
      if (layer.sublayers) {
        layer.sublayers.forEach(function(sublayer) {
          sublayer.visible = true;
        });
      }
    });
  }
};

// 适用对象：buildingSceneLayer ,还原初始状态
const filter = async (view, layerName, filters) => {
  const buildingLayer = layerUtils.getLayerByTitle(view, layerName);
  if (buildingLayer) {
    setFullModel(buildingLayer);
    let filterExpression = '';
    // 如果有过滤条件，则通过条件过滤，如果没有则还原到初始状态
    if (filters) {
      const params = filters.map(name => `'${name}'`);
      console.log(params);
      filterExpression = `ObjectId in (${params})`;
      console.log(filterExpression);
    }

    const [BuildingFilter] = await jsapi.load(['esri/layers/support/BuildingFilter']);
    const buildingFilter = new BuildingFilter({
      filterBlocks: [
        {
          filterExpression: filterExpression,
          filterMode: {
            type: 'solid',
          },
        },
      ],
    });
    buildingLayer.filters = [buildingFilter];
    buildingLayer.activeFilterId = buildingFilter.id;
  }
};

// 过滤出building scenelayer中 全部的build-component类型的图层
const findBulidingComponentSublayers = buildingLayer => {
  const buildingComponentsLayers = [];
  buildingLayer.allSublayers.items.forEach(item => {
    if (item.type === 'building-component') {
      buildingComponentsLayers.push(item);
    }
  });
  return buildingComponentsLayers;
};

// 通过记录的renderer 将模型还原为原来的样式
const refreshByRenderer = (view, layerName) => {
  const buildingLayer = layerUtils.getLayerByTitle(view, layerName);
  const resultLayers = findBulidingComponentSublayers(buildingLayer);
  resultLayers.forEach((sub, key) => {
    sub.renderer = sublayersRenderers[key];
  });
};

// 恢复图层的初始状态
const refresh = async (view, layerName) => {
  const buildingLayer = layerUtils.getLayerByTitle(view, layerName);
  const opt = {
    type: 'building',
    title: buildingLayer.title,
    url: buildingLayer.url,
  };
  view.map.remove(buildingLayer);
  const currentLayer = await layerCreator.create(opt);
  view.map.add(currentLayer);
  // add(view, opt);
};

// 对buildingscenelayer做唯一值渲染
const renderer = (view, layerName, filters, isFirst = true) => {
  const buildingLayer = layerUtils.getLayerByTitle(view, layerName);
  const resultLayers = findBulidingComponentSublayers(buildingLayer);
  const valueInfos = [];
  if (filters) {
    resultLayers.forEach(function(sub) {
      filters.forEach(objectid => {
        const info = {
          value: `${objectid}`, // code for interstates/freeways
          symbol: isFirst ? hotSym : yellowSym,
        };
        valueInfos.push(info);
      });
      const hwyRenderer = {
        type: 'unique-value', // autocasts as new UniqueValueRenderer()
        defaultSymbol: otherSym,
        field: 'ObjectId',
        uniqueValueInfos: valueInfos,
      };
      // console.log(hwyRenderer)
      sub.renderer = hwyRenderer;
    });
  } else {
    refresh(view, layerName);
  }
};



const buildingUtils = {
  add,
  filter,
  setFullModel,
  renderer,
  refresh,
};

export default buildingUtils;
